﻿/*!	@file	r_resource_loader_texture_win32.cpp
	@brief	テクスチャリソース読み込み(Win32ネイティブ
Copyright (c) 2011 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "r_resource_loader_texture_win32.h"
#include "../../../util/r_util_str.h"
#include "../../../stream/r_stream_reader.h"

#include "../../../draw/r_draw_texture2d_desc.h"

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../../../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace resource{
		namespace texture{
RRTTI_REFLECTION_IMPLEMENT ( r::resource::texture::LoaderPluginWin32, r::resource::texture::LoaderPlugin )
// <<<<<<<<<< RTTI <<<<<<<<<<

			LoaderPlugin* LoaderPluginWin32 :: createLoaderPlugin( cRAllocBase* allocator )
			{
				return new( allocator->malloc(sizeof(LoaderPluginWin32)) )LoaderPluginWin32( allocator );
			}

			void LoaderPluginWin32 :: destroyLoaderPlugin( cRAllocBase* allocator, LoaderPlugin* plugin )
			{
				plugin->~LoaderPlugin();
				allocator->free( plugin );
			}

			LoaderPluginWin32 :: LoaderPluginWin32( cRAllocBase* myAllocator )
			: LoaderPlugin( myAllocator )
			{
			}

			LoaderPluginWin32 :: ~LoaderPluginWin32()
			{
			}

			LoadCheckResult LoaderPluginWin32 :: checkLoadEnable( const r_cstr name, r::stream::Reader* stream )
			{
				r_ui32 len = r_tm_strlen( name );
				const r_char* fext = name;
				while( *fext != _RST('.') ){
					if( len == 0 ){
						break;
					}
					len--;
					fext++;
				}
				// ネイティブで処理可能なのは.ddsとか
				const r_cstr enableExts[] = {
					_RST(".dds"),
					_RST(".tga"),
					_RST(".bmp"),
					NULL
				};
				for( r_ui32 i = 0; enableExts[i] != NULL; i++ ){
					if( r_tm_strcmp( fext, enableExts[i] ) ){
						return LoadCheckResult_EnableWithCreateType;
					}
				}
				return LoadCheckResult_Disable;
			}

			r_bool LoaderPluginWin32 :: load( const r_cstr name, r::stream::Reader* stream, r::draw::Texture2DDesc& desc, void*& loadedData, size_t& size )
			{
				// 普通にファイルを読み込んでそれを設定する
				size = stream->getSize();
				loadedData = getAllocator()->malloc( size );
				stream->read( loadedData );

				desc.bindFlags			= r::draw::ImageBindFlag_ShaderResource;
				desc.usage				= r::draw::ImageUsageType_default;
				return true;
			}

			void LoaderPluginWin32 :: cleanup( void* loadedData )
			{
				// データを捨てる
				getAllocator()->free( loadedData );
			}

		}
	}
}